#!/bin/bash

if ( [ "$#" = 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ); then
  echo "
DESCRIPTION
  Command line interface to the Laia handwritten text recognition (HTR) toolkit
  installed in a docker image. It assumes that docker and nvidia-docker are
  installed, that these can be executed without sudo and that the corresponding
  image is tagged as laia:active. It will only work if executed from and
  referencing only files in the host's /Users, /home, /mnt, /media or /tmp
  directories.

  The interface can also be used to execute custom scripts inside the container,
  e.g. to create a model with a different architecture than what create-model
  provides. For this to work, the custom script must be located in a directory
  accessible to the container (see above) and be in the \$DPATH variable (same
  syntax as PATH) or be referenced with its full or relative path.

SYNOPSIS
  laia-docker COMMAND ARGUMENTS

  COMMAND is any of the Laia command line tools (optionally excluding the laia-
  prefix), e.g. create-model, train-ctc, decode. To see the detailed help of a
  command, just provide --help as argument, e.g. laia-docker decode --help.
";
  exit 0;
fi

CMD=("$@");
case "${CMD[0]}" in
  create-model|decode|force-align|netout|reuse-model|train-ctc) CMD[0]="laia-${CMD[0]}";;
esac
CMD=("${CMD[@]//$'\x27'/$'\x27\x22\x27\x22\x27'}");
CMD=("${CMD[@]/#/$'\x27'}");
CMD=("${CMD[@]/%/$'\x27'}");

OPTS=( --user $(id -u):$(id -g) );
OPTS+=( --rm );
OPTS+=( --workdir "$(pwd)" );
OPTS+=( --interactive );
if ! ( [ "${#CMD[@]}" = 1 ] && [ "${CMD[0]}" = "'bash'" ] ); then
  OPTS+=( --log-driver none );
  OPTS+=( --env TERM=xterm-256color );
fi
[ -d "/Users" ] && OPTS+=( -v /Users:/Users );
[ -d "/home" ]  && OPTS+=( -v /home:/home );
[ -d "/mnt" ]   && OPTS+=( -v /mnt:/mnt );
[ -d "/media" ] && OPTS+=( -v /media:/media );
[ -d "/tmp" ]   && OPTS+=( -v /tmp:/tmp );

[ "$DPATH" != "" ] && DPATH="PATH=\"$DPATH:\$PATH\" ";

if [ "${#CMD[@]}" = 1 ] && [ "${CMD[0]}" = "'bash'" ]; then
  [ "$DPATH" != "" ] && DPATH="export $DPATH;";
  TMP=$(mktemp laia-docker.init_XXXXXX);
  echo "
    export PS1='\h:\w\$ ';
    $DPATH
    alias ls='ls --color=auto';
    rm $TMP;
    " > $TMP;
  nvidia-docker run -t "${OPTS[@]}" laia:active \
    bash --init-file $TMP;
else
  nvidia-docker run "${OPTS[@]}" laia:active \
    bash -c "$DPATH${CMD[*]}";
fi
